from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



driver = webdriver.Chrome()
driver.get('http://172.16.0.205/shop/index.php?controller=simple&action=login')

# 操作用户名输入框
username_locator = (By.NAME,'login_info')
username = WebDriverWait(driver,15).until(EC.presence_of_element_located(username_locator))
username.clear()
username.send_keys('ceshi2001')

# 操作密码输入框
password_locator = (By.NAME,'password')
password = WebDriverWait(driver,15).until(EC.presence_of_element_located(password_locator))
password.clear()
password.send_keys('ceshi2001')




# 操作 登录按钮
submit_locator = (By.CLASS_NAME,'submit_login')
submit = WebDriverWait(driver,15).until(EC.presence_of_element_located(submit_locator))
submit.click()

# 验证登录结果
expected_url = 'http://172.16.0.205/shop/index.php?controller=ucenter&action=index'
ret_url = driver.current_url
try:
    ret_locator = (By.CLASS_NAME,'f14')
    result = WebDriverWait(driver,15).until(EC.presence_of_element_located(ret_locator))
    print('登录成功') if ret_url == expected_url and result.text == '您好，ceshi2001 欢迎回来!' else print('登录失败')
except:
    print('由于用户名或密码错误，导致登录失败！')


sleep(3)
driver.quit()